package com.zt.controller;

import com.zt.pojo.JsonUtils;
import com.zt.pojo.Setmeal;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("send")
public class SendController implements RabbitTemplate.ConfirmCallback {
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("add")
    public String send() {
        List<Setmeal> list = new ArrayList<>();
        Setmeal setmeal1 = new Setmeal(1, "入职体检", "入职体检是好的", "18-60", 38.6, "xcv123.jpg");
        Setmeal setmeal2 = new Setmeal(2, "婚检", "婚检是好的", "18-60", 88.6, "xcv123.jpg");
        Setmeal setmeal3 = new Setmeal(3, "体检", "体检是好的", "18-60", 26.6, "xcv123.jpg");
        Setmeal setmeal4 = new Setmeal(4, "开发", "开发是好的", "18-60", 38.6, "xcv123.jpg");
        rabbitTemplate.setConfirmCallback(this);
        CorrelationData correlationData=new CorrelationData();

        list.add(setmeal1);
        list.add(setmeal2);
        list.add(setmeal3);
        list.add(setmeal4);
        correlationData.setId(Objects.requireNonNull(JsonUtils.objectToJson(list)));
        for (Setmeal setmeal : list) {
            amqpTemplate.convertAndSend("setmeal_exchange", "a.b", JsonUtils.objectToJson(setmeal));
        }
        return "添加成功";
    }

    @RequestMapping("delete")
    public void delete() {
        rabbitTemplate.setConfirmCallback(this);
        amqpTemplate.convertAndSend("setmeal_exchange2", "a.delete", 2);
    }

    @RequestMapping("update")
    public String update() {
        rabbitTemplate.setConfirmCallback(this);
        Setmeal setmeal1 = new Setmeal(1, "入职体检要去正规三甲医院...", "入职体检是好的", "18-60", 38.6, "xcv123.jpg");
        amqpTemplate.convertAndSend("setmeal_exchange3", "a.update", JsonUtils.objectToJson(setmeal1));
        return "修改成功";
    }

    @RequestMapping("get/{id}")
    public String get(@PathVariable("id") Integer id) {
        amqpTemplate.convertAndSend("setmeal_exchange4", "a.get", id);
        return "查询成功";
    }

    @RequestMapping("page")
    public String page() {
        amqpTemplate.convertAndSend("setmeal_exchange5", "a.page", 1);
        return "分页查询成功";
    }

    @RequestMapping("like")
    public String like() {
        rabbitTemplate.setConfirmCallback(this);
        amqpTemplate.convertAndSend("setmeal_exchange6", "a.like", 1);
        return "模糊查询成功";
    }

    @RequestMapping("between/{name}/{min}/{max}")
    public String between(@PathVariable("name") String name,@PathVariable("min")Integer min,@PathVariable("max")Integer max) {
        Map<Object,Object> map=new HashMap<>();
        map.put("name",name);
        map.put("min",min);
        map.put("max",max);
        amqpTemplate.convertAndSend("setmeal_exchange7","a.between",JsonUtils.objectToJson(map));
        return "查询成功";
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean b, String s) {
        System.out.println(b);
    }
}
